package com.getpebble.android.common.core.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.res.Resources;
import com.getpebble.android.R;

public class PblPreferences {
    private SharedPreferences mPreferences;
    private final Resources mResources;

    public enum PrefKey {
        VERSION(R.string.pref_key_version),
        DEVELOPER_CONNECTION(R.string.pref_key_developer_connection_enable),
        DEVELOPER_CONNECTION_ACTIVE(R.string.pref_key_developer_connection_active),
        MUSIC_TARGET(R.string.pref_key_music_target),
        SYNC_INTERVAL(R.string.pref_key_sync_interval),
        RELEASE_CHANNEL(R.string.pref_key_release_channel),
        NAGGED_ABOUT_DISCONNECT(R.string.pref_key_nagged_about_disconnect),
        NAGGED_ABOUT_CONNECT_IS_DISCONNECT(R.string.pref_key_nagged_about_connect_is_disconnect),
        NAGGED_ABOUT_ENABLING_NOTIFICATION_LISTENER(R.string.pref_key_nagged_enable_notification_listener),
        SMS_NOTIFICATIONS(R.string.pref_key_sms_notifications),
        CALL_NOTIFICATIONS(R.string.pref_key_call_notifications),
        FIRMWARE_NAG_TIME(R.string.pref_key_firmware_nag_time),
        GMAIL_NOTIFICATION_HEADER(R.string.pref_key_gmail_notifications_header),
        ALL_APPS(R.string.pref_key_all_apps),
        ALLOW_THIRD_PARTY_NOTIFICATIONS(R.string.pref_key_allow_3rd_party_apps),
        ALWAYS_NOTIFY(R.string.pref_key_always_notify),
        MIGRATED_TO_HOLO(R.string.pref_key_migrated_to_holo),
        ONBOARDING_COMPLETED(R.string.pref_key_has_completed_onboarding),
        CALENDAR_NOTIFICATIONS(R.string.pref_key_calendar_reminders),
        ANALYTICS_OPTIN(R.string.pref_key_analytics_optin),
        TUTORIAL_COMPLETED(R.string.pref_key_has_completed_tutorial),
        GMAIL_ACCOUNT_ENABLED_PREFIX(R.string.pref_key_gmail_account_enabled_prefix),
        GMAIL_ACCOUNT_UID_PREFIX(R.string.pref_key_gmail_account_uid_prefix),
        INSTALLED_PEBBLE_APP_VERSION(R.string.pref_key_installed_pebble_app_version),
        DATALOGGING_DEBUG(R.string.pref_key_datalogging_debug),
        LAST_BOOTCONFIG_SYNC_MILLIS(R.string.pref_key_last_bootconfig_sync_millis),
        NOTIFICATION_ID_SEQUENCE(R.string.pref_key_notification_id_sequence),
        LANGUAGE_PACK_CATEGORY(R.string.pref_key_language_pack),
        AUTO_APP_UPDATES_ENABLED(R.string.pref_key_auto_app_updates_enabled),
        AUTO_APP_UPDATES_NOTIFICATIONS(R.string.pref_key_auto_app_updates_notifications),
        AUTO_APP_UPDATES_WIFI_ONLY(R.string.pref_key_auto_app_updates_wifi_only),
        DONE_NOTIFICATION_PREFS_TO_DB_MIGRATION(R.string.pref_key_done_notification_prefs_to_db_migration),
        VERBOSE_LOGCAT(R.string.pref_key_verbose_logcat),
        HEX_DUMP(R.string.pref_key_hex_dump),
        DONE_NOTIFICATION_PREFS_TO_DB_MIGRATION_WITH_DELETION(R.string.pref_key_done_notification_prefs_to_db_migration_with_deletion),
        RECEIVED_POTENTIAL_WEAR_ENHANCED_NOTIFICATION(R.string.pref_key_received_potential_wear_enhanced_notification),
        USER_SEEN_PEBBLE_ONLY_SCREEN(R.string.pref_key_user_seen_pebble_only_screen);
        
        final int resId;

        private PrefKey(int resId) {
            this.resId = resId;
        }

        public int getResId() {
            return this.resId;
        }
    }

    public PblPreferences(Context context) throws IllegalArgumentException {
        this.mPreferences = getSharedPreferences(context);
        this.mResources = context.getResources();
    }

    public static SharedPreferences getSharedPreferences(Context context) throws IllegalArgumentException {
        if (context != null) {
            return context.getSharedPreferences("PEBBLE_PREFERENCES", 4);
        }
        throw new IllegalArgumentException("Cannot retrieve shared preferences with null context");
    }

    public SharedPreferences getSharedPreferences() {
        return this.mPreferences;
    }

    public String getStringData(PrefKey key, String defaultString) {
        return this.mPreferences.getString(prefKeyToString(key), defaultString);
    }

    public long getLongData(PrefKey key, long defaultLong) {
        return this.mPreferences.getLong(prefKeyToString(key), defaultLong);
    }

    public void setLongData(PrefKey key, long data) {
        this.mPreferences.edit().putLong(prefKeyToString(key), data).apply();
    }

    public boolean getBooleanData(PrefKey key, boolean defaultBoolean) {
        return this.mPreferences.getBoolean(prefKeyToString(key), defaultBoolean);
    }

    public void setBooleanData(PrefKey key, boolean data) {
        this.mPreferences.edit().putBoolean(prefKeyToString(key), data).apply();
    }

    public void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener preferenceChangeListener) {
        this.mPreferences.registerOnSharedPreferenceChangeListener(preferenceChangeListener);
    }

    public void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener preferenceChangeListener) {
        this.mPreferences.unregisterOnSharedPreferenceChangeListener(preferenceChangeListener);
    }

    public String prefKeyToString(PrefKey key) {
        return this.mResources.getString(key.getResId());
    }
}
